{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [],
   "source": [
    "glsl=[]\r\n",
    "with open(\"D:/GitHub/Bilibili_Anime4K/Anime4K_Upscale_CNN_M_x2_Deblur.glsl\", \"r\") as f:\r\n",
    "    glsl=f.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [],
   "source": [
    "frags=[]\n",
    "buffer=[]\n",
    "\n",
    "for idx, line in enumerate(glsl):\n",
    "    if \"//!DESC\" in line or idx == len(glsl) - 1:\n",
    "        if \"//!DESC\" not in line:\n",
    "            buffer.append(line)\n",
    "        if \"//!DESC\" in buffer[0]:\n",
    "            frags.append(buffer)\n",
    "        buffer=[]\n",
    "    buffer.append(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "['HOOKED', 'NATIVE']\n['HOOKED', 'LUMAN0', 'NATIVE']\n['HOOKED', 'LUMAN1', 'NATIVE']\n['HOOKED', 'LUMAN2', 'NATIVE']\n['HOOKED', 'LUMAN3', 'NATIVE']\n['HOOKED', 'LUMAN4', 'NATIVE']\n['HOOKED', 'LUMAN1', 'LUMAN2', 'LUMAN3', 'LUMAN4', 'LUMAN5', 'NATIVE']\n['HOOKED', 'NATIVE']\n['HOOKED', 'MMKERNEL', 'NATIVE']\n['HOOKED', 'LUMAN0', 'MMKERNEL', 'NATIVE']\n"
     ]
    }
   ],
   "source": [
    "def tex(name):\n",
    "    return \"vec4 \" + name + \"_tex(vec2 pos) {\\n\"+\\\n",
    "        \"    return texture2D(\" + name + \", pos);\\n\"+\\\n",
    "        \"}\\n\"\n",
    "\n",
    "for i in range(len(frags)):\n",
    "    defines={}\n",
    "    hook_name=\"\"\n",
    "    is_in_body=False\n",
    "    \n",
    "    global_variables=[]\n",
    "    frag_names=[]\n",
    "    frag_signatures=[]\n",
    "\n",
    "    for j in range(len(frags[i])):\n",
    "        if \"//!\" in frags[i][j]:\n",
    "            if \"DESC\" in frags[i][j]:\n",
    "                hook_name=frags[i][j].split()[-1]\n",
    "                frag_names.append(hook_name)\n",
    "            if \"HOOK\" in frags[i][j] or \"BIND\" in frags[i][j]:\n",
    "                global_variables.append(frags[i][j].split()[-1])\n",
    "            continue\n",
    "            \n",
    "        if \"#define\" in frags[i][j]:\n",
    "            defines[frags[i][j].split()[1]] = frags[i][j].split()[2]\n",
    "            frags[i][j] = \"\\n\"\n",
    "            continue\n",
    "        \n",
    "        for origin, replacement in defines.items():\n",
    "            frags[i][j] = frags[i][j].replace(origin, replacement)\n",
    "        \n",
    "        if \"vec4 hook()\" in frags[i][j]:\n",
    "            is_in_body = True\n",
    "            type_name = \"void\"\n",
    "\n",
    "            frags[i][j] = frags[i][j].replace(\"vec4 hook()\", type_name + \" main()\")\n",
    "            frag_signatures.append(type_name + \" main()\")\n",
    "        \n",
    "        if is_in_body:\n",
    "            frags[i][j] = frags[i][j].replace(\"return\", \"gl_FragColor =\")\n",
    "\n",
    "    global_variables=sorted(set(global_variables))\n",
    "    print(global_variables)\n",
    "\n",
    "    generated_global_declarations=[]\n",
    "    generated_global_helper=[]\n",
    "\n",
    "    for j in global_variables:\n",
    "        generated_global_declarations.append(\"uniform sampler2D \" + j + \";\\n\")\n",
    "        generated_global_helper.append(tex(j))\n",
    "\n",
    "    generated_global_declarations.append(\"uniform vec2 u_size;\\nvarying vec2 v_tex_pos;\\n\")\n",
    "\n",
    "    generated_shader = [] + [\"precision mediump float;\\n\"] + generated_global_declarations + [\"\\n\"] + generated_global_helper + [\"\\n\"] + frags[i]\n",
    "\n",
    "    with open(\"D:/GitHub/Bilibili_Anime4K/Frags/\" + str(i) + \".glsl\", \"w\") as f:\n",
    "        f.writelines(generated_shader)"
   ]
  }
 ]
}